/// @tags: MatrixTree Exclusion
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const N = 18, mod = 1e9 + 7;

int n, all;
int m[N], u[N][N * N], v[N][N * N];
int *a[N], _a[N][N];

inline ll qpow(ll base, int exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1) res = res * base % mod;
    exp >>= 1;
    base = base * base % mod;
  }
  return res;
}

inline int det(int **a) {
  ll res = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = i; j <= n; ++j)
      if (a[j][i]) {
        if (j != i) res = res ? mod - res : res, swap(a[j], a[i]);
        break;
      }
    if (a[i][i] == 0) return 0;
    res = res * a[i][i] % mod;
    ll t = qpow(a[i][i], mod - 2);
    for (int j = i; j <= n; ++j) a[i][j] = t * a[i][j] % mod;
    for (int j = i + 1; j <= n; ++j) {
      t = mod - a[j][i];
      for (int k = i; k <= n; ++k) a[j][k] = (a[j][k] + t * a[i][k] % mod) % mod;
    }
  }
  return res;
}

inline int main() {
  cin >> n;
  all = 1 << (n - 1);
  for (int i = 1; i < n; ++i) {
    cin >> m[i];
    for (int j = 1; j <= m[i]; ++j) cin >> u[i][j] >> v[i][j];
  }
  ll ans = 0;
  for (int i = 1; i < all; ++i) {
    int cnt = 0;
    memset(_a, 0, sizeof(_a));
    for (int j = 1; j <= n; ++j) a[j] = _a[j];
    for (int j = 1; j < n; ++j) {
      if ((i >> (j - 1)) & 1) {
        cnt++;
        for (int k = 1; k <= m[j]; ++k) {
          int U = u[j][k], V = v[j][k];
          a[U][U]++, a[V][V]++;
          if (--a[U][V] < 0) a[U][V] += mod;
          if (--a[V][U] < 0) a[V][U] += mod;
        }
      }
    }
    ans += ((n - cnt - 1) & 1) ? mod - det(a) : det(a);
    if (ans >= mod) ans -= mod;
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P4336 [SHOI2016] 黑暗前的幻想乡.in", "r", stdin);
  freopen("P4336 [SHOI2016] 黑暗前的幻想乡.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
